package cn.school.controller.publicPhone;

import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.publicPhone.service.StudentCallService;
import cn.school.publicPhone.vo.StudentCallQuota;
import cn.school.publicPhone.vo.StudentCallRecords;
import cn.school.publicPhone.vo.StudentCallRecordsVo;
import cn.school.student.vo.StudentContactsImportVO;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @className: StudentCallsController
 * @author: Alex
 * @date: 2023/6/25
 **/
@RestController
@RequestMapping("/student/call")
public class StudentCallsController {

	@Autowired
	private StudentCallService studentCallService;

	private static Boolean lockByExcelImport = false;

	/**
	 * 查询通话记录列表
	 * @author alex
	 * @date 2023/6/26 8:28
	 * @return
	 */
	@PostMapping(value = "/getRecordsList")
	public CommonResult<CommonPage<StudentCallRecords>> getRecordsList(@RequestBody StudentCallRecordsVo vo) {
		CommonPage<StudentCallRecords> records = studentCallService.getRecordsList(vo);
		return CommonResult.success(records);
	}

	/**
	 * 获取通话额度配置列表
	 * @author alex
	 * @date 2023/6/26 8:54
	 * @return
	 */
	@PostMapping(value = "/getCallQuotaList")
	public CommonResult<CommonPage<StudentCallQuota>> getCallQuotaList(@RequestBody StudentCallRecordsVo vo) {
		CommonPage<StudentCallQuota> quotaList = studentCallService.getCallQuotaList(vo);
		return CommonResult.success(quotaList);
	}

	/**
	 * 通话额度设置
	 *
	 * @return
	 * @author alex
	 * @date 2023/6/25 8:25
	 */
	@PostMapping(value = "/saveCallQuota")
	public CommonResult<String> callQuotaSettings(@RequestBody List<StudentCallQuota> callQuotas) {
		Boolean result = studentCallService.setCallQuota(callQuotas);
		if (result) {
			return CommonResult.success("新增成功");
		}
		return CommonResult.failed("新增失败");
	}

	/**
	 * 获取剩余通话时长
	 * @author alex
	 * @date 2023/6/25 8:26
	 * @return
	 */
	@GetMapping(value = "/getRemainingDuration")
	public CommonResult<String> getRemainingDuration(@RequestBody String studentId) {
		String duration = studentCallService.getRemainingDuration(studentId);
		return CommonResult.success(duration);
	}

	/**
	 * 删除通话额度配置
	 * @author alex
	 * @date 2023/6/26 17:05
	 * @return
	 */
	@DeleteMapping(value = "/deleteByIds/{ids}")
	public CommonResult<Boolean> deleteByIds(@PathVariable Long[] ids) {
		List<Long> list = Arrays.stream(ids).collect(Collectors.toList());
		Boolean result = studentCallService.deleteQuotaByIds(list);
		return CommonResult.success(result);
	}

	@ApiOperation("importTemplate => 下载模板")
	@GetMapping("/importTemplate")
	public void importTemplate(HttpServletResponse response) throws IOException {
		StudentCallQuota callQuota = new StudentCallQuota();
		callQuota.setStudentId("学号");
		callQuota.setStudentName("学生姓名");
		callQuota.setStudentClass("班级名称");
		callQuota.setStudentCallQuota("通话额度(秒)");
		List<StudentCallQuota> list = new ArrayList<>();
		list.add(callQuota);
		EasyExcel.write(response.getOutputStream(), StudentCallQuota.class).sheet("额度配置").doWrite(list);
	}

	@PostMapping("/importData")
	public CommonResult<String> importStudentFromExcel(@RequestParam("file") MultipartFile multipartFile) throws IOException, ApiException {
		synchronized (lockByExcelImport) {
			if (this.lockByExcelImport) {
				new ApiException("有用户正在导入，请稍后重试");
			} else {
				this.lockByExcelImport = true;
			}
		}

		try {
			return studentCallService.importContactsList(multipartFile.getInputStream());
		} catch (ApiException apiException) {
			throw apiException;
		} catch (Exception e) {
			throw new ApiException("导入失败，请联系管理员");
		} finally {
			synchronized (lockByExcelImport) {
				this.lockByExcelImport = false;
			}
		}

	}
}
